[#ftl]
[#import "common/macrolibs.ftl" as lib]
[#import "common/commonclass.ftl" as cls]
package ${lib.packageBase}.dao;

[#if (clazz.mtmRelations?size>0)]
import java.util.List;
[/#if]

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.smzd.core.dao.AbstractDao;
[#if (clazz.mtmRelations?size>0)]
import com.smzd.core.domain.Many2Many;
[/#if]
import com.smzd.core.mappers.IMapper;
import ${lib.packageBase}.domain.${cls.mySimpleClassName};
import ${lib.packageBase}.mappers.${cls.mySimpleClassName}Mapper;
[#if (clazz.mtmRelations?size>0)]
[#if clazz.scopeClass??]
import ${lib.packageBase}.domain.${cls.myScopeClassName};
[/#if]
import com.smzd.core.utils.db.${cls.myPageName};
[@lib.importOwnerClass level=clazz.ownerLevel/]

[/#if]

@Component
public class ${cls.mySimpleClassName}Dao extends AbstractDao<${cls.mySimpleClassName}> {
    @Autowired
    private ${cls.mySimpleClassName}Mapper mapper;
    
    @Override
    protected IMapper<${cls.mySimpleClassName}> getMapper() {
        return mapper;
    }
    [#if (clazz.mtmRelations?size>0)]
    [#list clazz.mtmRelations as mtm]
    [#assign masterClassName][@lib.simpleClassName clazz=mtm.master/][/#assign]
    [#assign slaveClassName][@lib.simpleClassName clazz=mtm.slave/][/#assign]
    [#assign configView][@lib.moduleName clazz=mtm.master/][@lib.moduleNames clazz=mtm.slave/].ftl[/#assign]
    [#assign masterId]${masterClassName?uncap_first}Id[/#assign]
    [#assign slaveId]${cls.mySimpleClassName?uncap_first}Id[/#assign]
    public ${cls.myPageName}${cls.myPageParam} getOuter${slaveClassName}ListBy${masterClassName}(${cls.myPageName}${cls.myPageParam} page) {
        preparePage(page);
        List<${cls.mySimpleClassName}> elems = mapper.getOuter${slaveClassName}ListBy${masterClassName}(page);
        page.setElems(elems);
        int total = mapper.getElemsCount(page);
        page.setTotalCount(total);
        completePage(page);
        return page;
    }

    public void save${masterClassName}${slaveClassName}Relations(List<Many2Many> toAdd, List<Many2Many> toDel) {
        for(Many2Many add : toAdd){
            mapper.insert${masterClassName}${slaveClassName}Relation(add);
        }
        for(Many2Many del : toDel){
            mapper.remove${masterClassName}${slaveClassName}Relation(del);
        }
    }
    [/#list]
    [/#if]
}